import { api, checkResponse } from '@/utils/api';
import service from '@/utils/request';
import {
  FILE_TYPE,
  FILE_CONTENT_TYPE,
  FILE_SUFFIX,
} from '@/utils/constants';

const url = '/v1/storage/create/';

const createFile = (fileType, prefix, filename = '') => {
  if (fileType === FILE_TYPE.FOLDER) {
    prefix += `${filename}/`;
    filename = '.';
  } else {
    filename = filename.endsWith(FILE_SUFFIX[fileType]) ? filename : filename + FILE_SUFFIX[fileType];
  }
  return service.post(`/api${url}`, {
    id: 1,
    params: {
      filename,
      contentType: FILE_CONTENT_TYPE[fileType],
      bucket: 'lion',
      prefix,
    },
  });
};

const getListData = (prefix, bucket = 'lion') =>
/* api.list({ prefix, bucket }).then((response) => {
      if (response.Code === 200) {
        this.list = response.Result
        console.log('list', response.Result);
      }
    }) */
  api.list({ prefix, bucket }).then(checkResponse);

const getStarData = (object, bucket = 'lion') => {
  api.stat({ object, bucket }).then((response) => {
    if (response.Code === 200) {
      console.log('star', response.Result);
    }
  });
};

const moveAjaxHandle = (objectOpts, bucket = 'lion') => api.move({ objectOpts, bucket });

const movedirAjaxHandle = (objects, bucket = 'lion') => api.movedir({ objects, bucket });

const deleteDirAjaxHandle = (objects = []) => api.deleteDir({ objects });

const deleteAjaxHandle = (objects = []) => api.delete({ objects });

const copyAjaxHandle = (objectSrc, objectDst, bucket = 'lion') => { //
  api.copy({ objectSrc, objectDst, bucket }).then((response) => {
    if (response.Code === 200) {
      console.log('copy', response.Result);
    }
  });
};

const moveToTrash = (objects) => api.moveToTrash({ objects });

const restoreFormTrash = (objects) => api.restore({
  objects,
});

export {
  createFile,
  getListData,
  getStarData,
  moveAjaxHandle,
  movedirAjaxHandle,
  deleteDirAjaxHandle,
  deleteAjaxHandle,
  copyAjaxHandle,
  moveToTrash,
  restoreFormTrash,
};
